const { readFile, unlink, writeFile } = require('node:fs/promises')
// const { copy } = require('fs-extra')


const copyData = [
  'plugin.json',
  'preload.js',
  'js',
  'mdFile',
  'markdown-fill.png'
]


const copyFiles = async ( ) => {
  for(let i in copyData){
   try{
    await copy(`${__dirname}\\${copyData[i]}`,`${__dirname}\\dist\\${copyData[i]}`)
   }catch(err){console.log(err);}
  }
}

const flieContent = async (path) => {
  const content = await readFile(path,'utf8')
  console.log(content);
  const src = content.match(/<script\stype="module"\scrossorigin.+\.js/g)
  await unlink(`${__dirname}\\dist\\assets\\${src[0].split('/').at(-1)}`)
  console.log('------',src[0].split('/').at(-1),'-------------');
  const withModule = content.replace(/<script\stype="module".+<\/script>/g,'');
  const withNomodule = withModule.replace(/nomodule\scrossorigin/g, '');
  const withSrc = withNomodule.replace(/data-src/g, 'src');
  const withScript = withSrc.replace(/System.+\)/g, '')
  console.log('---------------------------------------------------------');
  console.log(withScript);

  return withScript
 }


 const resetContent = async () => {
  
  const content = await flieContent(`${__dirname}\\dist\\index.html`)
  await writeFile(`${__dirname}\\dist\\index.html`, content, 'utf8')
 } 



 (async () => {
  // await copyFiles()
  await resetContent()
 })()




